package com.gofun.framework.core.utils;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class GsonUTCdateAdapter implements JsonSerializer<Date>, JsonDeserializer<Date> {

	@Override
	public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
			throws JsonParseException {

		try {
			String s = json.getAsString();
			final DateFormat dateFormat = getFormat(s);

			return dateFormat.parse(s);
		} catch (ParseException e) {
			throw new JsonParseException(e);
		}

	}

	@Override
	public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
		final DateFormat dateFormat = getFormat();
		return new JsonPrimitive(dateFormat.format(src));
	}

	private DateFormat getFormat(String s) {
		DateFormat dateFormat;
		String format = null;
		if (s == null) {
			return null;
		} else if (s.length() == 10) {
			format = "yyyy-MM-dd";
		} else if (s.length() == 19) {
			format = "yyyy-MM-dd' 'HH:mm:ss";
		} else if (s.length() == 23) {
			format = "yyyy-MM-dd' 'HH:mm:ss.SSS";
		} else {
			throw new JsonParseException("Date format error:" + s);
		}

		dateFormat = new SimpleDateFormat(format);
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
		return dateFormat;
	}

	private DateFormat getFormat() {
		String format = "yyyy-MM-dd' 'HH:mm:ss";
		DateFormat dateFormat = new SimpleDateFormat(format);
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
		return dateFormat;
	}

}
